﻿using PlcServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Common;
using Model;
using Server;

namespace BearingOiling
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private Servers servers { get; set; }
        private IFreeSql DB = Servers.DB;
        private List<Bearings> BearingTotals { get; set; } = new List<Bearings>();

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            PlcHelper.InitPlc();
            PlcHelper.MsgConnectionStatus += PlcHelper_MsgConnectionStatus;
            PlcHelper.PushTagValEvent += PlcHelper_PushTagValEvent;
            servers = new Servers();
            servers.RecordDoing += FreshData;
            servers.Start();
            Task.Run(() =>
            {
                //List<Bearings> models;
                //try
                //{
                //    models = DB.Select<Bearings>().ToList();
                //    this.Dispatcher.BeginInvoke(new Action(() =>
                //    {
                //        DataGrid.ItemsSource = models;
                //    }));
                //}
                //catch (Exception exception)
                //{
                //    Console.WriteLine(exception);

                //}
            });
        }

        private void PlcHelper_PushTagValEvent(string arg1, int arg2)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (arg1 == "Trigger")
                {
                    TextBlockTrigger.Text = arg2.ToString();
                    return;
                }

                if (arg1 == "CMD")
                {
                    TextBlockCmd.Text = arg2.ToString();
                    return;
                }

                if (arg1 == "BearingNo")
                {
                    TextBlockBearingNo.Text = arg2.ToString();
                    return;
                }
            }));

        }

        private void PlcHelper_MsgConnectionStatus(string arg1, bool arg2)
        {
            if (arg1 != "1#PLC")
            {
                return;
            }

            Dispatcher.BeginInvoke(new Action(() =>
            {
                this.Dispatcher.BeginInvoke(new Action(() => { this.PlcStu.Background = arg2 ? new SolidColorBrush(Color.FromArgb(255, 173, 255, 47)) : new SolidColorBrush(Color.FromArgb(255, 255, 99, 71)); ; }));
            }), null);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var view = CfgView.GetView();
            view.Show();
            view.Focus();
        }

        private void RcdSelect_OnClick(object sender, RoutedEventArgs e)
        {
            var view = RecordView.GetView();
            view.Show();
            view.Focus();

        }
        /// <summary>
        /// 刷新表格数据
        /// </summary>
        private void FreshData(string no, float oilnum)
        {
            try
            {
                //默认查到主表中必须存在这个编号的轴承总量信息
                var rlt = DB.Select<Bearings>().Where(p => p.BearingNo == no).First();
                rlt.OilNum = oilnum;
                BearingTotals.Insert(0, rlt);
                if (BearingTotals.Count > 30)
                {
                    BearingTotals.RemoveAt(BearingTotals.Count-1);
                }

                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    DataGrid.ItemsSource = null;
                    DataGrid.ItemsSource = BearingTotals;
                }));
            }
            catch (Exception e)
            {
                Logger.WriteSysLog("刷新界面数据失败", e);
            }
        }
    }
}
